package com.icinfo.cloud.provider.punish.ucase.investigateevidence.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.common.documentoperate.enums.DocumentStatusEnum;
import com.icinfo.cloud.provider.punish.config.service.IConfigFastProcedureService;
import com.icinfo.cloud.provider.punish.config.vo.ConfigFastProcedureDocumentInfoVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.CaseStageEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.CaseTypeEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseDocumentRecordMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseInfoAdjustRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseReasonMainRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseDocumentRecordKindInfoVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseMainInfoVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseReasonMainRecordVo;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.service.IInvestigateService;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.vo.InvestigateIsEditNextStageVo;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述: 调查取证阶段 对应的Service接口实现类.<br>
 *
 * @author shishengyao
 * @description
 * @date 2023/4/21
 */
@Service
public class InvestigateServiceImpl implements IInvestigateService {
    @Resource
    private ICaseInfoAdjustRecordService caseInfoAdjustRecordService;
    @Resource
    private ICaseMainInfoService caseMainInfoService;
    @Resource
    private IConfigFastProcedureService configFastProcedureService;
    @Resource
    private ICaseReasonMainRecordService caseReasonMainRecordService;
    @Resource
    private CaseDocumentRecordMapper caseDocumentRecordMapper;

    @Override
    public InvestigateIsEditNextStageVo isEditNextStage(String caseId) {
        UserDTO userInfo = UserUtil.getUserInfo();
        InvestigateIsEditNextStageVo nextStageVo = new InvestigateIsEditNextStageVo();
        nextStageVo.setIfEditNextStage(true);
        //存在调整中的当事人
        if (caseInfoAdjustRecordService.isExistUndoneAdjustPartyRecord(caseId)) {
            nextStageVo.setIfEditNextStage(false);
            nextStageVo.setErrorMessage("请操作完调整当事人步骤（审批通过或）删除调整当事人审批表");
            return nextStageVo;
        }

        //快速办理案件.调查取证阶段校验
        CaseMainInfoVo caseMainInfo = caseMainInfoService.getCaseMainInfo(caseId);
        if (CaseTypeEnum.SIMPLE_QUICK.getCode().equals(caseMainInfo.getCaseType()) && CaseStageEnum.INVESTIGATION.getCode().equals(caseMainInfo.getCaseCurrentStageCode())) {
            CaseReasonMainRecordVo caseReasonMainRecordVo = caseReasonMainRecordService.getCaseReasonRecordByCaseId(caseId);
            //获取快速办理配置
            List<CaseDocumentRecordKindInfoVo> caseDocumentRecordList = caseDocumentRecordMapper.selectCaseDocumentRecordWithKindInfoByByStageCode(caseId, CaseStageEnum.INVESTIGATION.getCode(), caseMainInfo.getBusiParamId());
            List<ConfigFastProcedureDocumentInfoVo> requiredDocumentList = new ArrayList<>();
            List<ConfigFastProcedureDocumentInfoVo> configFastProcedureDocumentInfoVoList = configFastProcedureService.getDocumentTemplateByListCode(caseMainInfo.getCaseOrgCode(), caseReasonMainRecordVo.getCaseReasonCode(), caseId);
            List<String> filterDocumentRecordList = new ArrayList<>();
            filterDocumentRecordList.add("11");
            if (ObjectUtil.isNotEmpty(caseDocumentRecordList)) {
                filterDocumentRecordList = caseDocumentRecordList.stream().filter(e -> !"1".equals(e.getIsConfirm()) || DocumentStatusEnum.CONFIRM_END.getCode().equals(e.getDocumentSignstampedStatus()))
                        .map(CaseDocumentRecordKindInfoVo::getDocumentKindCode).collect(Collectors.toList());
            }
            for (ConfigFastProcedureDocumentInfoVo procedureDocumentInfoVo : configFastProcedureDocumentInfoVoList) {
                if (Constant.STRING_1.equals(procedureDocumentInfoVo.getIsRequired()) && !filterDocumentRecordList.contains(procedureDocumentInfoVo.getDocumentKindCode())) {
                    requiredDocumentList.add(procedureDocumentInfoVo);
                }
            }

            if (ObjectUtil.isNotEmpty(requiredDocumentList) && requiredDocumentList.size() > 0) {
                nextStageVo.setIfEditNextStage(false);
                String requiredDocumentName = requiredDocumentList.stream().map(ConfigFastProcedureDocumentInfoVo::getDocumentShowName).collect(Collectors.joining("、"));
                nextStageVo.setErrorMessage(requiredDocumentName + "文书未制作完成");
            }
        }
        return nextStageVo;
    }


}
